/* @(#)GSFileUploadProcessor.java    V0.1
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-2-20
 *   Tuthmos Liu  2008-06-13 增加获取图件号记录信息
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.business;

import java.io.DataInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import system.LoginContext;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSManufactureCategoryBean;
import com.hitrust.gs.bean.GSManufactureFilterUploadBean;
import com.hitrust.gs.bean.GSProviderBean;
import com.hitrust.gs.dao.GSManufactureCategoryDAO;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.dao.GSManufactureFilterUploadDAO;
import com.hitrust.gs.dao.GSProviderDAO;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseProcesser;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSManufactureCategoryUploadVO;
import com.hitrust.gs.vo.GSManufactureFilterUploadVO;
import com.hitrust.gs.vo.GSManufactureUploadVO;
import com.hitrust.gs.vo.GSProviderUploadVO;
import com.hitrust.gs.lib.GSSystemConfigurator;

import com.hitrust.gs.util.excel.PoiUtil;

/**
 * 导入Excel文件到数据库
 */
public class GSFileUploadProcessor implements GSBaseProcesser{
    /**
     * 批量导入产品信息
     * @param aFile
     * @throws CUPSException
     */
    public static String batchImportManufacture(DataInputStream aFile,String aUserName) throws GSException 
    {
        int tSuccCount = 0;
        int tFailCount = 0;
        String tFailFile = "0";
        HSSFWorkbook tHSSFWorkbook = new HSSFWorkbook();
        HSSFSheet tHSSFSheet = null;
        try{
            //开始读取excel文件
            tHSSFWorkbook = new HSSFWorkbook(aFile);           //读取sheet表单
            //读取sheet表单
            tHSSFSheet = tHSSFWorkbook.getSheetAt(0);
        }catch(Exception e){
            //e.printStackTrace();
            GSLogWriter.error(e.getMessage());
            throw new GSException("批量上传文件失败");
        }
            ArrayList tList = new ArrayList();
            ArrayList tErrList = new ArrayList();
            ArrayList tSuccList = new ArrayList();
            //读取并检查明细
            int tRowCount =getManufactureContents(tList,tHSSFSheet);
            GSManufactureDAO tGSManufactureDAO=new GSManufactureDAO();
            
            if(tList.size() > 0){
                for(int i=0; i<tList.size(); i++){
                    GSManufactureUploadVO tGSManufactureUploadVO = (GSManufactureUploadVO)tList.get(i);
                    GSManufactureBean tGSManufactureBean = tGSManufactureUploadVO.getGSManufactureBean();

                    int tResult = Integer.parseInt(tGSManufactureUploadVO.getInsert_result());
                    if(tResult == 1){
                        tGSManufactureUploadVO.setGSManufactureBean(tGSManufactureBean);
                       try{
                           //tGSManufactureBean.setGs_is_tradeflat_manufacture("1");
                           tGSManufactureBean.setGs_recorder_id(aUserName);
						   String tSetManufactureFilter = GSSystemConfigurator.getParam("gs.SetManufactureFilter");
                           //判断是否存在产品信息
                           if(new GSManufactureDAO().isExist(tGSManufactureBean.getGs_manufacture_id()))
                           {
                           	    tGSManufactureDAO.modify(tGSManufactureBean);   
                           }
                           else
                           {
                           		//产品导入是否需要筛选								
								boolean isAdd = true;
								if (tSetManufactureFilter.equalsIgnoreCase("true"))
								{
									if (!(new GSManufactureFilterUploadDAO()).isExist(tGSManufactureBean))
									{
										isAdd = false;
									}		
								}
								
                               	//新增产品信息
                                if (isAdd){
                                    tGSManufactureBean.setIs_buy("0");
                                    tGSManufactureDAO.add(tGSManufactureBean);
                                }
                                    
                                    
                                
                           }
                           tSuccCount ++;
                           //System.out.println(tSuccCount+"记录");
                           tGSManufactureUploadVO.setInsert_result("1");
                           tSuccList.add(tGSManufactureUploadVO);
                           
                         //为更新的产品表同步产品过滤表
                           tGSManufactureDAO.filter(tGSManufactureBean.getGs_manufacture_id());
                       }catch(Exception e){
                           GSLogWriter.info("ManufactureDown 第"+ (i+1) +"行出错!");
                           tFailCount++;
                           tGSManufactureUploadVO.setInsert_result("0");
                           tGSManufactureUploadVO.setFailure_reason(e.getMessage());
                           tErrList.add(tGSManufactureUploadVO);
                       }
                    }else if(tResult == 0){
                        tFailCount++;
                        tErrList.add(tGSManufactureUploadVO);
                    }
                    
                }
            }
            
            //将新增失败的商户信息写入文件
            if(tErrList.size() > 0){
                tFailFile = writeManufactureFile(tErrList,tSuccList,tHSSFWorkbook,tRowCount);
            }
            return tSuccCount+"-"+tFailCount+"-"+tFailFile;
        }

	
	/**
	 * 批量导入过滤产品信息
	 * @param aFile
	 * @throws CUPSException
	 */
	public static String batchImportFilterManufacture(DataInputStream aFile,String aUserName) throws GSException 
	{
		int tSuccCount = 0;
		int tFailCount = 0;
		String tFailFile = "0";
		HSSFWorkbook tHSSFWorkbook = new HSSFWorkbook();
		HSSFSheet tHSSFSheet = null;
		try{
			//开始读取excel文件
			tHSSFWorkbook = new HSSFWorkbook(aFile);           //读取sheet表单
			//读取sheet表单
			tHSSFSheet = tHSSFWorkbook.getSheetAt(0);
		}catch(Exception e){
			//e.printStackTrace();
			GSLogWriter.error(e.getMessage());
			throw new GSException("批量导入过滤产品失败");
		}
			
		ArrayList tList = new ArrayList();
		ArrayList tErrList = new ArrayList();
		ArrayList tSuccList = new ArrayList();
		//读取并检查明细
		int tRowCount =getManufactureFilterContents(tList,tHSSFSheet);
		GSManufactureFilterUploadDAO tGSManufactureFilterUploadDAO=new GSManufactureFilterUploadDAO();
            
		if(tList.size() > 0)
		{
			for(int i=0; i<tList.size(); i++)
			{
				GSManufactureFilterUploadVO tGSManufactureFilterUploadVO = (GSManufactureFilterUploadVO)tList.get(i);
				GSManufactureFilterUploadBean tGSManufactureFilterUploadBean = tGSManufactureFilterUploadVO.getGSManufactureFilterUploadBean();

				int tResult = Integer.parseInt(tGSManufactureFilterUploadVO.getInsert_result());
				if(tResult == 1)
				{
					tGSManufactureFilterUploadVO.setGSManufactureFilterUploadBean(tGSManufactureFilterUploadBean);
					try
					{
						//判断是否存在产品信息
						if(new GSManufactureFilterUploadDAO().isExist(tGSManufactureFilterUploadBean.getGs_manufacture_id()))
						{
							//更新产品信息
							tGSManufactureFilterUploadDAO.modify(tGSManufactureFilterUploadBean);   
						}
						else
						{
							//新增产品信息
							tGSManufactureFilterUploadDAO.add(tGSManufactureFilterUploadBean);
						}
						tSuccCount ++;
						//System.out.println(tSuccCount+"记录");
						tGSManufactureFilterUploadVO.setInsert_result("1");
						tSuccList.add(tGSManufactureFilterUploadVO);
					}
					catch(Exception e)
					{
						tFailCount++;
						tGSManufactureFilterUploadVO.setInsert_result("0");
						tGSManufactureFilterUploadVO.setFailure_reason(e.getMessage());
						tErrList.add(tGSManufactureFilterUploadVO);
					}
				}
				else if(tResult == 0)
				{
					tFailCount++;
					tErrList.add(tGSManufactureFilterUploadVO);
				}
                    
			}
		}
		
		//将导入失败的筛选产品信息写入文件
		if(tErrList.size() > 0){
			tFailFile = writeManufactureFilterUploadFile(tErrList,tSuccList,tHSSFWorkbook,tRowCount);
		}
		
		return tSuccCount+"-"+tFailCount+"-"+tFailFile;
	}
    
    /**
     * 读取excel产品信息文档内容
     * @param aList             ArrayList对象，存放读到的内容
     * @param aSheet            表单
     * @throws CUPSException
     */
    private static int getManufactureContents(ArrayList aList,HSSFSheet aHSSFSheet) throws GSException{
        int i = 0;
        //取得第一行的对象
        HSSFRow tFirstRow = aHSSFSheet.getRow(0);
        DecimalFormat a = new DecimalFormat("#");
        while(true){
           HSSFRow tHSSFRow = aHSSFSheet.getRow(1 + i);
            boolean flag = false;
            try{
                
                for(int j=0; j<19; j++){
                   if(tHSSFRow == null){
                        break;
                    }
                    if(tHSSFRow.getCell((short)j) == null){
                        break;
                    }
                   flag=true;
                   break;
                }
                if(!flag){
                    break;
                }else{
                    GSManufactureUploadVO tGSManufactureUploadVO=new GSManufactureUploadVO();
                    int tResult = 1;
                    String tFailReason = "";
                    GSManufactureBean tGSManufactureBean = new GSManufactureBean();

                    for(int j=0; j<=20; j++){
                       //获得EXCEL首行记录
                       String tFiledName = tFirstRow.getCell((short)j).getStringCellValue();
                        //产品编号
                        if(tFiledName.equals("ProductID")){
                            String tProductID= String.valueOf(tHSSFRow.getCell((short)j).getStringCellValue());
                            //String tProductID= String.valueOf(tHSSFRow.getCell((short)j).getNumericCellValue());
                            //对数字格式化
                            //tProductID=a.format(tHSSFRow.getCell((short)j).getNumericCellValue());
                            //if (new GSManufactureDAO().isExist(tProductID)) {
                           //    tFailReason = tFailReason + "[产品编号]已经存在，您不能再添加|";
                           //    tResult = 0 ;
                          //  }else{
                            tGSManufactureBean.setGs_manufacture_id(tProductID);
                          //  }
                         //集中编码   
                        }else if(tFiledName.equals("CusItemCode")){
                            String tCusItemCode = tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSManufactureBean.setGs_concentricity(tCusItemCode);
                            
                         //产品名称
                        }else if(tFiledName.equals("ProductName")){
                            String tProductName = tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSManufactureBean.setGs_manufacture_name(tProductName);
                            
                          //产品种类编号  
                        }else if(tFiledName.equals("CategoryCode")){
                            String tCategoryCode = String.valueOf(tHSSFRow.getCell((short)j).getStringCellValue());
                            //tCategoryCode=tCategoryCode.substring(0,tCategoryCode.indexOf("."));
                            tGSManufactureBean.setGs_manufacture_category_id(tCategoryCode);
                            
                        //产品品牌  
                        }else if(tFiledName.equals("ProductBrand")){
                            String tProductBrand = tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSManufactureBean.setGs_manufacture_brand(tProductBrand);
                        
                         //产品型号   
                        }else if(tFiledName.equals("ProductType")){
                            String tProductType = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSManufactureBean.setGs_manufacture_type(tProductType);
                            
                         //产品规格  
                        }else if(tFiledName.equals("ProductSpec")){
                            String tProductSpec = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSManufactureBean.setGs_manufacture_spec(tProductSpec);
                            
                         //产品单价
                        }else if(tFiledName.equals("ProductPrice")){
                           if(tHSSFRow.getCell((short)j) != null && !tHSSFRow.getCell((short)j).toString().equals("")){
                                String tProductPrice = String.valueOf(tHSSFRow.getCell((short)j).getNumericCellValue());
                                tGSManufactureBean.setGs_manufacture_unitprice(Double.parseDouble(tProductPrice));
                            }
                        //数量单位  
                        }else if(tFiledName.equals("ProductUnit")){
                            String tProductUnit = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSManufactureBean.setGs_quant_unit(tProductUnit);
                            
                        //质保期   
                        }else if(tFiledName.equals("AssurePeriod")){
                            String tAssurePeriod = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSManufactureBean.setGs_quality_assure_period(tAssurePeriod);
                            
                        //生产厂商名称    
                        }else if(tFiledName.equals("Manufacturer")){
                            String tManufacturer = tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSManufactureBean.setGs_manufacturer_name(tManufacturer);
                            
                        //产地   
                        }else if(tFiledName.equals("MakeArea")){
                            String tMakeArea = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSManufactureBean.setGs_make_area(tMakeArea);
                            
                         //供应商编号
                        }else if(tFiledName.equals("CompanyID")){
                            String tCompanyID = String.valueOf(tHSSFRow.getCell((short)j).getNumericCellValue());
                            tCompanyID=tCompanyID.substring(0,tCompanyID.indexOf("."));
                            tGSManufactureBean.setGs_provider_id(tCompanyID);
                             
                        //适用机型    
//                        }else if(tFiledName.equals("MachineType")){
//                           if(!(tHSSFRow.getCell((short)j)==null)){
//                           String tMachineType = tHSSFRow.getCell((short)j).getStringCellValue().trim();
//                           tGSManufactureBean.setGs_adapt_machine_type(tMachineType);
//                           }
                        //产品图片  
                        }else if(tFiledName.equals("ProductImage")){
                            String tProductImage = tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            if(tProductImage.equals(null)){
                                tGSManufactureBean.setGs_manufacture_images("");    
                            }else{
                                tGSManufactureBean.setGs_manufacture_images(tProductImage);
                            }
                        //最近调价价格时间  
                        }else if(tFiledName.equals("RecentPriceChange")){
                            String tRecentPriceChange = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSManufactureBean.setGs_recent_adjustprice_date(tRecentPriceChange);
                        }
                        //最近调价价格
                        else if(tFiledName.equals("RecentPrice")){
                            if(tHSSFRow.getCell((short)j) != null && !tHSSFRow.getCell((short)j).toString().equals("")){
                                String tRecentPrice = String.valueOf(tHSSFRow.getCell((short)j).getNumericCellValue());
                                tGSManufactureBean.setGs_recent_adjusted_price(Double.parseDouble(tRecentPrice));
                            }
           
                        }
                        //是否交易平台的产品
                        else if(tFiledName.equals("InWarehouse")){
                            if(tHSSFRow.getCell((short)j) != null && !tHSSFRow.getCell((short)j).toString().equals("")){
                                String tInWarehouse = String.valueOf(tHSSFRow.getCell((short)j).getNumericCellValue());
                                tInWarehouse=tInWarehouse.substring(0,tInWarehouse.indexOf("."));
                                tGSManufactureBean.setGs_is_tradeflat_manufacture(tInWarehouse);
                            }
           
                        }
                        //产品附加属性
                        else if(tFiledName.equals("ExProperties")){
                            String tExProperties=tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSManufactureBean.setGs_appender_property(tExProperties);
                        }
                        //备注
                        else if(tFiledName.equals("Remark")){
                            String tRemark=tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSManufactureBean.setGs_remark(tRemark);
                        }
                      //图件号
                        else if(tFiledName.equals("PicNo")){
                            String tPicNo=tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSManufactureBean.setGs_drawing_peice_code(tPicNo);
                        }
                        //是否有效
                        else if(tFiledName.equals("Validity")){
                        	double v = tHSSFRow.getCell((short)j).getNumericCellValue();
                            String tValidity=Integer.toString((int)v);
                            tGSManufactureBean.setGs_validity(tValidity);
                        }
                    }
                    tGSManufactureUploadVO.setGSManufactureBean(tGSManufactureBean);
                    tGSManufactureUploadVO.setFailure_reason(tFailReason);
                    tGSManufactureUploadVO.setInsert_result(String.valueOf(tResult));
                    tGSManufactureUploadVO.setRowNumber(i+1);
                    aList.add(tGSManufactureUploadVO);
                    i++;
                }
            }catch(RuntimeException e){
                GSLogWriter.error("****Manufacture BatchImport RuntimeEx :["+e.getMessage()+"]");
                throw new GSException("导入数据中某一列数据类型不符合规定.");
            }catch(Error e){
                GSLogWriter.error("****Manufacture BatchImport ErroeEx :["+e.getMessage()+"]");
                throw new GSException(e.getMessage());
            }catch(Exception e){
                GSLogWriter.error("****Manufacture BatchImport Ex :["+e.getMessage()+"]");
                throw new GSException(e.getMessage());
            }   
           }
       return i+2;
       }
    
    /**
     * 写入产品信息导入错误原因
     * @param aErrList
     * @param aSuccList
     * @param aBookIn
     * @param aRowCount
     * @return 
     * @throws GSException
     */
    
    private static String writeManufactureFile(ArrayList aErrList,ArrayList aSuccList,HSSFWorkbook aBookIn,int aRowCount) throws GSException{
        String tOutputPathName = "0";
         try{
            //取得工作薄
             HSSFSheet tHSSFSheet = aBookIn.getSheetAt(0);
             //删除导入成功的记录
             for(int i = 0 ; i < aSuccList.size() ; i++){
                 GSManufactureUploadVO tSuccVO = (GSManufactureUploadVO)aSuccList.get(i);
                 tHSSFSheet.removeRow(tHSSFSheet.getRow(tSuccVO.getRowNumber()));
             }
             //将失败原因写入错误文件
             for(int j = 0 ; j < aErrList.size() ; j++ ){
                 GSManufactureUploadVO tErrVO = (GSManufactureUploadVO)aErrList.get(j);
                 HSSFRow tHSSFRow = tHSSFSheet.getRow(tErrVO.getRowNumber());
                 HSSFCell[] tCell = new HSSFCell[19];
                 tCell[18] = tHSSFRow.getCell((short)18);
                 tCell[18].setEncoding(HSSFCell.ENCODING_UTF_16);
                 tCell[18].setCellValue(tErrVO.getFailure_reason());
                 int k = tErrVO.getRowNumber();
                 if((k-j-1) != 0){
                     tHSSFSheet.shiftRows(k,k,-(k-j-1),true,true);
                     tHSSFSheet.removeRow(tHSSFSheet.getRow(k));
                 }
             }
//
        //  }
             //读取输出文件路径
             String tDay = DateUtil.getNextUpDay(0);
             String tOutputPath = GSSystemConfigurator.getParam("gs.down.savepath");
             String tFileName="Manufacture_"+tDay;
             //设置输出文件名字
             tOutputPathName = tOutputPath + "/" + tFileName  +".xls";
             java.io.FileOutputStream tOutput = new java.io.FileOutputStream(tOutputPathName);
             //输出错误信息文件
             aBookIn.write(tOutput);
             tOutput.flush();
             tOutput.close();
             
         }catch(Exception e){
             GSLogWriter.error(e.getMessage());
             return tOutputPathName;
         }
         return tOutputPathName;
    }
    
    /**
     * 批量导入产品种类
     * @param aFile
     * @throws CUPSException
     */
    public static String batchImportManufactureCategory(DataInputStream aFile,String aUserName,LoginContext logincontext) throws GSException {
//        if (aFile.getFileSize() == 0) {
//            throw new GSException("当前文件不存在或为空");
//        }
//        if((aFile.getFileName().toLowerCase().lastIndexOf("xls") == -1)){
//            throw new GSException("文件格式错误");
//        }
//        String tFileName2=aFile.getFileName().substring(0,aFile.getFileName().indexOf("_"));
//        if(!tFileName2.equals("Category")){
//            throw new GSException("导入文件不是产品种类");
//        }
//        if ( aFile.getFileSize() > 3145728 ){
//           throw new GSException("上传文件不能超过3M");
//        }

        int tSuccCount = 0;
        int tFailCount = 0;
        String tFailFile = "0";
        HSSFWorkbook tHSSFWorkbook = new HSSFWorkbook();
        HSSFSheet tHSSFSheet = null;
        try{
            //开始读取excel文件
            tHSSFWorkbook = new HSSFWorkbook(aFile);           //读取sheet表单
            //读取sheet表单
            tHSSFSheet = tHSSFWorkbook.getSheetAt(0);
        }catch(Exception e){
            //e.printStackTrace();
            GSLogWriter.error(e.getMessage());
            throw new GSException("批量上传文件失败");
        }
            ArrayList tList = new ArrayList();
            ArrayList tErrList = new ArrayList();
            ArrayList tSuccList = new ArrayList();
            //读取并检查明细
            int tRowCount =getManufactureCategoryContents(tList,tHSSFSheet);
            
            if(tList.size() > 0){
                for(int i=0; i<tList.size(); i++){
                    GSManufactureCategoryUploadVO tGSManufactureCategoryUploadVO = (GSManufactureCategoryUploadVO)tList.get(i);
                    GSManufactureCategoryBean tGSManufactureCategoryBean = tGSManufactureCategoryUploadVO.getGSManufactureCategoryBean();
                    GSManufactureCategoryDAO tGSManufactureCategoryDAO=new GSManufactureCategoryDAO();
                    int tResult = Integer.parseInt(tGSManufactureCategoryUploadVO.getInsert_result());
                    if(tResult == 1){
                        tGSManufactureCategoryUploadVO.setGSManufactureCategoryBean(tGSManufactureCategoryBean);
                       try{
                           //从交易平台导入
                           tGSManufactureCategoryBean.setGs_is_tradeflat_manuf_category("0");
                           
                           if(tGSManufactureCategoryBean.getGs_manufacture_level().equals("3")){
                               tGSManufactureCategoryBean.setGs_upper_manufacture_category(tGSManufactureCategoryBean.getGs_manufacture_category_id2());
                           }
                           else{
                               tGSManufactureCategoryBean.setGs_upper_manufacture_category(tGSManufactureCategoryBean.getGs_manufacture_category_id1());
                           }
                           
                           //判断是否存在种类
                           if (new GSManufactureCategoryDAO().isExist(tGSManufactureCategoryBean.getGs_manufacture_category_id())){
                               //更新产品种类
                               tGSManufactureCategoryDAO.modify(tGSManufactureCategoryBean,logincontext);
                               System.out.println("UPDATE"+i);
                           }else{
//                             //新增产品种类信息
                               tGSManufactureCategoryDAO.add(tGSManufactureCategoryBean,logincontext);
                           }
                           tSuccCount ++;
                           tGSManufactureCategoryUploadVO.setInsert_result("1");
                           tSuccList.add(tGSManufactureCategoryUploadVO);
                       }catch(Exception e){
                           GSLogWriter.info("ManufactureCategoryDown 第"+ (i+1) +"行出错!");
                           tFailCount++;
                           tGSManufactureCategoryUploadVO.setInsert_result("0");
                           tGSManufactureCategoryUploadVO.setFailure_reason(e.getMessage());
                           tErrList.add(tGSManufactureCategoryUploadVO);
                       }
                    }else if(tResult == 0){
                        tFailCount++;
                        tErrList.add(tGSManufactureCategoryUploadVO);
                    }
                    
                }
            }
            //将新增失败的商户信息写入文件
            if(tErrList.size() > 0){
                tFailFile = writeManufactureCategoryFile(tErrList,tSuccList,tHSSFWorkbook,tRowCount);
            }
            return tSuccCount+"-"+tFailCount+"-"+tFailFile;
        }
    
    /**
     * 读取excel供应商内容
     * @param aList             ArrayList对象，存放读到的内容
     * @param aSheet            表单
     * @throws CUPSException
     */
    private static int getManufactureCategoryContents(ArrayList aList,HSSFSheet aHSSFSheet) throws GSException{
        int i = 0;
        //取得第一行的对象
        HSSFRow tFirstRow = aHSSFSheet.getRow(0);
        
        while(true){
           HSSFRow tHSSFRow = aHSSFSheet.getRow(1 + i);
            boolean flag = false;
            try{
                
                for(int j=0; j<6; j++){
                   if(tHSSFRow == null){
                        break;
                    }
                    if(tHSSFRow.getCell((short)j) == null){
                        break;
                    }
                    if(tHSSFRow.getCell((short)j).getStringCellValue() != null && !tHSSFRow.getCell((short)j).getStringCellValue().equals("")){
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    break;
                }else{
                    GSManufactureCategoryUploadVO tGSManufactureCategoryUploadVO = new GSManufactureCategoryUploadVO();
                   
                    int tResult = 1;
                    String tFailReason = "";
                    GSManufactureCategoryBean tGSManufactureCategoryBean = new GSManufactureCategoryBean();
                    
                    
                    for(int j=0; j<6; j++){
                       //获得EXCEL首行记录
                       String tFiledName = tFirstRow.getCell((short)j).getStringCellValue();
                        //产品种类编号
                        if(tFiledName.equals("CategoryCode")){
                            String tCategoryCode = tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            //if (new GSManufactureCategoryDAO().isExist(tCategoryCode)) {
                             //   tFailReason = tFailReason + "[产品种类编号]已经存在，您不能再添加|";
                             //   tResult = 0 ;
                            // }else{
                                 tGSManufactureCategoryBean.setGs_manufacture_category_id(tCategoryCode);
                            // }
                           
                         //产品种类名称   
                        }else if(tFiledName.equals("CategoryName")){
                            String tCategoryName = tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSManufactureCategoryBean.setGs_manufacture_category_name(tCategoryName);
                            
                         //产品种类描述
                        }else if(tFiledName.equals("CategoryDes")){
                            String tCategoryDes = tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSManufactureCategoryBean.setGs_manufacture_desc(tCategoryDes);
                            
                          //产品种类级别  
                        }else if(tFiledName.equals("CategoryLevel")){
                            String tCategoryLevel = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSManufactureCategoryBean.setGs_manufacture_level(tCategoryLevel);
                            
                        //产品种类一级编号  
                        }else if(tFiledName.equals("FirstCategory")){
                            if(tHSSFRow.getCell((short)j) != null){
                              //String tFirstCategory = String.valueOf(tHSSFRow.getCell((short)j).getNumericCellValue());
                              //tFirstCategory=tFirstCategory.substring(0,tFirstCategory.indexOf("."));
                               String tFirstCategory=tHSSFRow.getCell((short)j).getStringCellValue();
                                tGSManufactureCategoryBean.setGs_manufacture_category_id1(tFirstCategory);
                            }
                        }
                        //产品种类二级编号
                        else if(tFiledName.equals("SecondCategory")){
                            if(tHSSFRow.getCell((short)j) != null){
                                //String tSecondCategory = String.valueOf(tHSSFRow.getCell((short)j).getNumericCellValue());
                               // tSecondCategory=tSecondCategory.substring(0,tSecondCategory.indexOf("."));
                                String tSecondCategory=tHSSFRow.getCell((short)j).getStringCellValue();
                                tGSManufactureCategoryBean.setGs_manufacture_category_id2(tSecondCategory);
                              }
                          }
                    }
                    tGSManufactureCategoryUploadVO.setGSManufactureCategoryBean(tGSManufactureCategoryBean);
                    tGSManufactureCategoryUploadVO.setFailure_reason(tFailReason);
                    tGSManufactureCategoryUploadVO.setInsert_result(String.valueOf(tResult));
                    tGSManufactureCategoryUploadVO.setRowNumber(i+1);
                    aList.add(tGSManufactureCategoryUploadVO);
                    i++;
                }
            }catch(RuntimeException e){
                GSLogWriter.error("****ManufactureCategory BatchImport RuntimeEx :["+e.getMessage()+"]");
                throw new GSException("导入数据中某一列数据类型不符合规定.");
            }catch(Error e){
                GSLogWriter.error("****ManufactureCategory BatchImport ErroeEx :["+e.getMessage()+"]");
                throw new GSException(e.getMessage());
            }catch(Exception e){
                GSLogWriter.error("****ManufactureCategory BatchImport Ex :["+e.getMessage()+"]");
                throw new GSException(e.getMessage());
            }   
           }
       return i+2;
       }
    
    /**
     * 写入产品种类导入错误原因
     * @param aErrList
     * @param aSuccList
     * @param aBookIn
     * @param aRowCount
     * @return 
     * @throws GSException
     */
    
    private static String writeManufactureCategoryFile(ArrayList aErrList,ArrayList aSuccList,HSSFWorkbook aBookIn,int aRowCount) throws GSException{
        String tOutputPathName = "0";
         try{
            //取得工作薄
             HSSFSheet tHSSFSheet = aBookIn.getSheetAt(0);
             //删除导入成功的记录
             for(int i = 0 ; i < aSuccList.size() ; i++){
                 GSManufactureCategoryUploadVO tSuccVO = (GSManufactureCategoryUploadVO)aSuccList.get(i);
                 tHSSFSheet.removeRow(tHSSFSheet.getRow(tSuccVO.getRowNumber()));
             }
             //将失败原因写入错误文件
             for(int j = 0 ; j < aErrList.size() ; j++ ){
                 GSManufactureCategoryUploadVO tErrVO = (GSManufactureCategoryUploadVO)aErrList.get(j);
                 HSSFRow tHSSFRow = tHSSFSheet.getRow(tErrVO.getRowNumber());
                 HSSFCell[] tCell = new HSSFCell[7];
                 tCell[5] = tHSSFRow.getCell((short)5);
                 tCell[5].setEncoding(HSSFCell.ENCODING_UTF_16);
                 tCell[5].setCellValue(tErrVO.getFailure_reason());
                 int k = tErrVO.getRowNumber();
                 if((k-j-1) != 0){
                     tHSSFSheet.shiftRows(k,k,-(k-j-1),true,true);
                     tHSSFSheet.removeRow(tHSSFSheet.getRow(k));
                 }
             }
//
        //  }
             //读取输出文件路径
             String tDay=DateUtil.getNextUpDay(0);
             String tOutputPath = GSSystemConfigurator.getParam("gs.down.savepath");
             String tFileName="ManufactureCategory_"+tDay;
             //设置输出文件名字
             tOutputPathName = tOutputPath + "/" + tFileName  +".xls";
             java.io.FileOutputStream tOutput = new java.io.FileOutputStream(tOutputPathName);
             //输出错误信息文件
             aBookIn.write(tOutput);
             tOutput.flush();
             tOutput.close();
             
         }catch(Exception e){
             GSLogWriter.error(e.getMessage());
             return tOutputPathName;
         }
         return tOutputPathName;
    }
    
    /**
     * 批量导入供应商信息
     * @param aFile
     * @throws CUPSException
     */
    public static String batchImportProvider(DataInputStream aFile,String aUserName) throws GSException {
//        if (aFile.getFileSize() == 0) {
//            throw new GSException("当前文件不存在或为空");
//        }
//        if((aFile.getFileName().toLowerCase().lastIndexOf("xls") == -1)){
//            throw new GSException("文件格式错误");
//        }
//        
//        String tFileName3=aFile.getFileName().substring(0,aFile.getFileName().indexOf("_"));
//        if(!tFileName3.equals("Company")){
//            throw new GSException("导入文件不是供应商信息");
//        }
//        if ( aFile.getFileSize() > 3145728 ){
//           throw new GSException("上传文件不能超过3M");
//        }

        int tSuccCount = 0;
        int tFailCount = 0;
        String tFailFile = "0";
        HSSFWorkbook tHSSFWorkbook = new HSSFWorkbook();
        HSSFSheet tHSSFSheet = null;
        try{
            //开始读取excel文件
            tHSSFWorkbook = new HSSFWorkbook(aFile);           //读取sheet表单
            //读取sheet表单
            tHSSFSheet = tHSSFWorkbook.getSheetAt(0);
        }catch(Exception e){
            //e.printStackTrace();
            GSLogWriter.error(e.getMessage());
            throw new GSException("批量上传文件失败");
        }
            ArrayList tList = new ArrayList();
            ArrayList tErrList = new ArrayList();
            ArrayList tSuccList = new ArrayList();
            //读取并检查明细
            int tRowCount =getProviderContents(tList,tHSSFSheet);
            
            if(tList.size() > 0){
                for(int i=0; i<tList.size(); i++){
                    GSProviderUploadVO tGSProviderUploadVO = (GSProviderUploadVO)tList.get(i);
                    GSProviderBean tGSProviderBean = tGSProviderUploadVO.getGSProviderBean();
                    GSProviderDAO tGSProviderDAO=new GSProviderDAO();
                    int tResult = Integer.parseInt(tGSProviderUploadVO.getInsert_result());
                    if(tResult == 1){
                        tGSProviderUploadVO.setGSProviderBean(tGSProviderBean);
                       try{
                           //从交易平台导入
                           tGSProviderBean.setGs_is_tradeflat_provider("0");
                           //判断供应商是否存在
                           if (new GSProviderDAO().isExist(tGSProviderBean.getGs_provider_id())){
                               //更新供应商信息
                               tGSProviderDAO.modify(tGSProviderBean);     
                           }else{
                               //新增供应商信息
                               tGSProviderDAO.add(tGSProviderBean);
                           }
                           tSuccCount ++;
                           tGSProviderUploadVO.setInsert_result("1");
                           tSuccList.add(tGSProviderUploadVO);
                       }catch(Exception e){
                           GSLogWriter.info("ProviderDown 第"+ (i+1) +"行出错!");
                           tFailCount++;
                           tGSProviderUploadVO.setInsert_result("0");
                           tGSProviderUploadVO.setFailure_reason(e.getMessage());
                           tErrList.add(tGSProviderUploadVO);
                       }
                    }else if(tResult == 0){
                        tFailCount++;
                        tErrList.add(tGSProviderUploadVO);
                    }
                    
                }
            }
            //将新增失败的供应商信息写入文件
            if(tErrList.size() > 0){
                tFailFile = writeProviderFile(tErrList,tSuccList,tHSSFWorkbook,tRowCount);
            }
            return tSuccCount+"-"+tFailCount+"-"+tFailFile;
        }
    
    /**
     * 读取excel供应商文档内容
     * @param aList             ArrayList对象，存放读到的内容
     * @param aSheet            表单
     * @throws CUPSException
     */
    private static int getProviderContents(ArrayList aList,HSSFSheet aHSSFSheet) throws GSException{
        int i = 0;
        //取得第一行的对象
        HSSFRow tFirstRow = aHSSFSheet.getRow(0);
        
        while(true){
           HSSFRow tHSSFRow = aHSSFSheet.getRow(1 + i);
            boolean flag = false;
            try{
                
                for(int j=0; j<6; j++){
                   if(tHSSFRow == null){
                        break;
                    }
                    if(tHSSFRow.getCell((short)j) == null){
                        break;
                    }
//                    if(tHSSFRow.getCell((short)j).getStringCellValue() != null && !tHSSFRow.getCell((short)j).getStringCellValue().equals("")){
//                        flag = true;
//                        break;
//                    }
                    flag = true;
                    break;
                }
                if(!flag){
                    break;
                }else{
                    GSProviderUploadVO tGSProviderUploadVO = new GSProviderUploadVO();
                   
                    int tResult = 1;
                    String tFailReason = "";
                    GSProviderBean tGSProviderBean = new GSProviderBean();
                    
                    
                    for(int j=0; j<=17; j++){
                       //获得EXCEL首行记录
                       String tFiledName = tFirstRow.getCell((short)j).getStringCellValue();
                        //供应商编号
                        if(tFiledName.equals("CompanyID")){
                            String tCompanyID = String.valueOf(tHSSFRow.getCell((short)j).getNumericCellValue());
                            tCompanyID=tCompanyID.substring(0,tCompanyID.indexOf("."));
                            //判断产品种类编号是否存在
                           // if (new GSProviderDAO().isExist(tCompanyID)) {
                           //     tFailReason = tFailReason + "[产品种类编号]已经存在，您不能再添加|";
                           //     tResult = 0 ;
                           //  }else{
                            tGSProviderBean.setGs_provider_id(tCompanyID);
                           // }
                           
                         //供应商简称   
                        }else if(tFiledName.equals("CompanyName")){
                            String tCompanyName = tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSProviderBean.setGs_provider_shortname(tCompanyName);
                            
                         //供应商全称
                        }else if(tFiledName.equals("CompanyFullName")){
                            String tCompanyFullName = tHSSFRow.getCell((short)j).getStringCellValue().trim();
                            tGSProviderBean.setGs_provider_name(tCompanyFullName);
                            
                          //供应商英文名称
                        }else if(tFiledName.equals("CompanyEngName")){
                            String tCompanyEngName = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_provider_engname(tCompanyEngName);
                            
                        //公司性质  
                        }else if(tFiledName.equals("CompanyProperty")){
                            if(tHSSFRow.getCell((short)j) != null){
                              String tCompanyProperty = tHSSFRow.getCell((short)j).getStringCellValue();
                              //tCompanyProperty=tCompanyProperty.substring(0,tParentCategory.indexOf("."));
                              tGSProviderBean.setGs_corp_property(tCompanyProperty);
                            }
                        }
                        //经营地址
                        else if(tFiledName.equals("Address")){
                           String tAddress = tHSSFRow.getCell((short)j).getStringCellValue();
                           tGSProviderBean.setGs_corp_addr(tAddress);
                        }
                        //邮政编码
                        else if(tFiledName.equals("Zipcode")){
                            String tZipcode = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_corp_post(tZipcode);
                         }
                        //负责人
                        else if(tFiledName.equals("ManagePerson")){
                            String tManagePerson = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_principal_id(tManagePerson);
                         }
                        //负责人联系电话
                        else if(tFiledName.equals("ManagerTel")){
                            String tManagerTel = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_principal_phone(tManagerTel);
                         }
                        //联系人
                        else if(tFiledName.equals("Contact")){
                            String tContact = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_contact_id(tContact);
                         }
                        //联系人电话
                        else if(tFiledName.equals("ContactTel")){
                            String tContactTel = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_contact_phone(tContactTel);
                         }
                        //所属国家
                        else if(tFiledName.equals("Country")){
                            String tCountry = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_reside_country(tCountry);
                         }
                        //所属省份
                        else if(tFiledName.equals("Province")){
                            String tProvince = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_reside_province(tProvince);
                         }
                        //所属城市
                        else if(tFiledName.equals("City")){
                            String tCity = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_reside_city(tCity);
                         }
                        //主营行业
                        else if(tFiledName.equals("Industry")){
                            String tIndustry = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_work_trade(tIndustry);
                         }
                        //主营产品
                        else if(tFiledName.equals("MainProduct")){
                            String tMainProduct = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_work_manufacture(tMainProduct);
                         }
                        //经营范围
                        else if(tFiledName.equals("Scope")){
                            String tScope = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_work_scope(tScope);
                         }
                        //有效性
                        else if(tFiledName.equals("Validity")){
                            String tValidity = null;
                            tValidity = tHSSFRow.getCell((short)j).getStringCellValue();
                            tGSProviderBean.setGs_validity(tValidity);
                         }
                    }
                    
                    tGSProviderUploadVO.setGSProviderBean(tGSProviderBean);
                    tGSProviderUploadVO.setFailure_reason(tFailReason);
                    tGSProviderUploadVO.setInsert_result(String.valueOf(tResult));
                    tGSProviderUploadVO.setRowNumber(i+1);
                    aList.add(tGSProviderUploadVO);
                    i++;
                }
            }catch(RuntimeException e){
                GSLogWriter.error("****Provider BatchImport RuntimeEx :["+e.getMessage()+"]");
                throw new GSException(e.getMessage());
            }catch(Error e){
                GSLogWriter.error("****Provider BatchImport ErroeEx :["+e.getMessage()+"]");
                throw new GSException(e.getMessage());
            }catch(Exception e){
                GSLogWriter.error("****Provider BatchImport Ex :["+e.getMessage()+"]");
                throw new GSException(e.getMessage());
            }   
           }
       return i+2;
       }
    
    /**
     * 写入供应商信息导入错误原因
     * @param aErrList
     * @param aSuccList
     * @param aBookIn
     * @param aRowCount
     * @return 
     * @throws GSException
     */
    
    private static String writeProviderFile(ArrayList aErrList,ArrayList aSuccList,HSSFWorkbook aBookIn,int aRowCount) throws GSException{
        String tOutputPathName = "0";
         try{
            //取得工作薄
             HSSFSheet tHSSFSheet = aBookIn.getSheetAt(0);
             //删除导入成功的记录
             for(int i = 0 ; i < aSuccList.size() ; i++){
                 GSProviderUploadVO tSuccVO = (GSProviderUploadVO)aSuccList.get(i);
                 tHSSFSheet.removeRow(tHSSFSheet.getRow(tSuccVO.getRowNumber()));
             }
             //将失败原因写入错误文件
             for(int j = 0 ; j < aErrList.size() ; j++ ){
                 GSProviderUploadVO tErrVO = (GSProviderUploadVO)aErrList.get(j);
                 HSSFRow tHSSFRow = tHSSFSheet.getRow(tErrVO.getRowNumber());
                 HSSFCell[] tCell = new HSSFCell[18];
                 tCell[17] = tHSSFRow.getCell((short)17);
                 tCell[17].setEncoding(HSSFCell.ENCODING_UTF_16);
                 tCell[17].setCellValue(tErrVO.getFailure_reason());
                 int k = tErrVO.getRowNumber();
                 if((k-j-1) != 0){
                     tHSSFSheet.shiftRows(k,k,-(k-j-1),true,true);
                     tHSSFSheet.removeRow(tHSSFSheet.getRow(k));
                 }
             }
//
        //  }
             //读取输出文件路径
             String tDay = DateUtil.getNextUpDay(0);
             String tOutputPath = GSSystemConfigurator.getParam("gs.down.savepath");
             String tFileName="Provider_"+tDay;
             //设置输出文件名字
             tOutputPathName = tOutputPath + "/" + tFileName  +".xls";
             java.io.FileOutputStream tOutput = new java.io.FileOutputStream(tOutputPathName);
             //输出错误信息文件
             aBookIn.write(tOutput);
             tOutput.flush();
             tOutput.close();
             
         }catch(Exception e){
             GSLogWriter.error(e.getMessage());
             return tOutputPathName;
         }
         return tOutputPathName;
    }

    
	/**
	 * 读取筛选产品excel产品信息文档内容
	 * @param aList             ArrayList对象，存放读到的内容
	 * @param aSheet            表单
	 * @throws CUPSException
	 */
	private static int getManufactureFilterContents(ArrayList aList,HSSFSheet aHSSFSheet) throws GSException
	{
		int i = 0;
		int k = 0;
		//取得第一行的对象
		HSSFRow tFirstRow = aHSSFSheet.getRow(0);
		while(true)
		{
			boolean flag = false;
			
		    HSSFRow tHSSFRow = aHSSFSheet.getRow(1 + k);
		    
		    k++;			
			try
			{
				for(int j=0; j<2; j++){
				   if(tHSSFRow == null){
						break;
					}
					if(tHSSFRow.getCell((short)j) == null){
						break;
					}
				    flag=true;
				    break;
				}
				
				if(!flag)
				{
					break;
				}
				else
				{				
					
					GSManufactureFilterUploadVO tGSManufactureFilterUploadVO = new GSManufactureFilterUploadVO();
					int tResult = 1;
					String tFailReason = "";
					GSManufactureFilterUploadBean tGSManufactureFilterUploadBean = new GSManufactureFilterUploadBean();
					    String tConcentricityCode = "";
	                    String tManufactureCode = "";
	                    String tIsGSUse = "0";
	                    String tBuyerID = "";
	                    String tColStr  = "";
	                    
	                    for(int m=0;m<tFirstRow.getLastCellNum();m++)
	                    {                       
	                      tColStr = PoiUtil.getCellValue(tFirstRow,m,"");
	                        
	                      if(tColStr.equalsIgnoreCase("采购商自编码"))
	                      {
	                          //集中编码
	                          tConcentricityCode = PoiUtil.getCellValue(tHSSFRow,m,"");   
	                          tManufactureCode = tConcentricityCode;
	                      }
	                      else if(tColStr.equalsIgnoreCase("采购商ID"))
	                      {
	                          //是本集中采购商                  
	                          tBuyerID = PoiUtil.getCellValue(tHSSFRow,m,"").trim(); 
	                          if (tBuyerID.equalsIgnoreCase(GSSystemConfigurator.getParam("gs.BuyerID")))
	                              tIsGSUse = "1";
	                          else
	                              tIsGSUse = "0";                            
	                      }
	                    }                                                               
	                    
	                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	                    String buf = df.format(new java.util.Date(System.currentTimeMillis()));                    
	                    
	                    tGSManufactureFilterUploadBean.setGs_manufacture_id(tConcentricityCode);
	                    tGSManufactureFilterUploadBean.setGs_concentricity_id(tConcentricityCode);
	                    tGSManufactureFilterUploadBean.setGs_inport_time(buf);
	 
	                    if (tIsGSUse.equalsIgnoreCase("1") && !tManufactureCode.equalsIgnoreCase(""))
	                    {   
	                        tGSManufactureFilterUploadVO.setGSManufactureFilterUploadBean(tGSManufactureFilterUploadBean);
	                        tGSManufactureFilterUploadVO.setFailure_reason(tFailReason);
	                        tGSManufactureFilterUploadVO.setInsert_result(String.valueOf(tResult));
	                        tGSManufactureFilterUploadVO.setRowNumber(k);
	                        aList.add(tGSManufactureFilterUploadVO);
	                        i++;
	                    }
	                    else
	                    {
	                        String tReson = "";
	                        if (!GSSystemConfigurator.getParam("gs.BuyerID").equalsIgnoreCase(tBuyerID))
	                        {
	                            tReson = "产品不属于该采购商"; 
	                        }		
						else
						{
							tReson = "产品数据格式错误！";
						}
						
						tGSManufactureFilterUploadVO.setGSManufactureFilterUploadBean(tGSManufactureFilterUploadBean);
						tGSManufactureFilterUploadVO.setFailure_reason(tReson);
						tGSManufactureFilterUploadVO.setInsert_result("0");
						tGSManufactureFilterUploadVO.setRowNumber(k);
						aList.add(tGSManufactureFilterUploadVO);
						i++;
					}
				}

			}catch(RuntimeException e){
				GSLogWriter.error("****GSFileUploadProcessor batchImportFilterManufacture RuntimeEx :["+e.getMessage()+"]");
				throw new GSException("导入数据中某一列数据类型不符合规定.");
			}catch(Error e){
				GSLogWriter.error("****GSFileUploadProcessor batchImportFilterManufacture ErroeEx :["+e.getMessage()+"]");
				throw new GSException(e.getMessage());
			}catch(Exception e){
				GSLogWriter.error("****GSFileUploadProcessor batchImportFilterManufacture Ex :["+e.getMessage()+"]");
				throw new GSException(e.getMessage());
			} 	   		
		}		
		return i;
	}
	
	/**
	 * 记录不符合的筛选产品信息
	 * @param aErrList
	 * @param aSuccList
	 * @param aBookIn
	 * @param aRowCount
	 * @return 
	 * @throws GSException
	 */
    
	private static String writeManufactureFilterUploadFile(ArrayList aErrList,ArrayList aSuccList,HSSFWorkbook aBookIn,int aRowCount) throws GSException
	{
		String tOutputPathName = "0";
		//读取输出文件路径
		String tOutputPath = GSSystemConfigurator.getParam("gs.down.savepath");
		String tFileName = "Failure_ManufactureFilterUpload_";
		try
		{
			//取得工作薄
			HSSFSheet tHSSFSheet = aBookIn.getSheetAt(0);
			//删除导入成功的记录
			for(int i = 0 ; i < aSuccList.size() ; i++)
			{
				GSManufactureFilterUploadVO tSuccVO = (GSManufactureFilterUploadVO)aSuccList.get(i);
				tHSSFSheet.removeRow(tHSSFSheet.getRow(tSuccVO.getRowNumber()));
			}
			//将失败原因写入错误文件
			for(int j = 0 ; j < aErrList.size() ; j++ )
			{
				GSManufactureFilterUploadVO tErrVO = (GSManufactureFilterUploadVO)aErrList.get(j);
				HSSFRow tHSSFRow = tHSSFSheet.getRow(tErrVO.getRowNumber());
				HSSFCell tCell = null;				
				tCell = tHSSFRow.createCell(tHSSFRow.getLastCellNum());
				tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
				tCell.setCellValue(tErrVO.getFailure_reason());
				int k = tErrVO.getRowNumber();
				if((k-j-1) != 0)
				{
					tHSSFSheet.shiftRows(k,k,-(k-j-1),true,true);
					tHSSFSheet.removeRow(tHSSFSheet.getRow(k));
				}
			}

			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			String buf = df.format(new java.util.Date(System.currentTimeMillis())); 
			
			tFileName += buf + ".xls";
			
			//设置输出文件名字
			tOutputPathName = tOutputPath + "/" + tFileName ;
			java.io.FileOutputStream tOutput = new java.io.FileOutputStream(tOutputPathName);
			//输出错误信息文件
			aBookIn.write(tOutput);
			tOutput.flush();
			tOutput.close();
             
		 }catch(Exception e){
			 GSLogWriter.error("记录不符合的筛选产品信息错误："+e.getMessage());
			 return "0";
		 }
		 return tFileName;
	}	
	
}

